package com.wuzhiming.istudy.controller;

import com.alibaba.fastjson.JSONObject;
import com.wuzhiming.istudy.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.hutool.crypto.digest.MD5;

import java.util.Date;

/**
 * @ClassName: LoginController
 * @Description: 登录模块控制器
 * @Author:zhimingw
 * @Version 1.0
 * @since 2022/3/16 18:44
 */
@Controller
public class LoginController extends BaseController{
    /**
    * @Description: 管理员登录
    * @Param: [name, password]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2022/3/28
    */
    @PostMapping("/adminLogin")
    @ResponseBody
    public String adminLogin(String name,String  password){
        //判断用户是否通过md5加密
        String salt = DigestUtils.md5DigestAsHex(password.getBytes());
        // 获取管理员对象 本系统中管理员是唯一的 id为1的用户
        User admin = userService.queryById(1);
        // 获取Md5加密对象
        MD5 md5 = new MD5(admin.getSalt().getBytes());
        //进行判断
        if(admin.getName().equals(name)&&admin.getPassword().equals(salt)){
            result.setCode(200);
            admin.setRecentLogin(new Date());
            userService.update(admin);
            session.setAttribute("admin",admin);
        }else{
            result.setCode(100);
        }
        return JSONObject.toJSONString(result);
    }
    
    
    @PostMapping("/login")
    @ResponseBody
    public String login(String name,String password){
        //判断用户是否通过md5加密
        String salt = DigestUtils.md5DigestAsHex(password.getBytes());
        //获取用户对象
        User getUser = new User(name,salt);
        User user = new User();
        user = userService.queryUser(getUser);
        if(user!=null){
            getUser.setStatus(1);
            User temp = userService.queryUser(getUser);
            if(temp==null){
                result.setCode(502);
                return JSONObject.toJSONString(result);
            }else{
                result.setCode(200);
                System.out.println("成功登录");
            }
        }else if(userService.queryByName(name)==null){ //用户未注册，默认注册
            if(userService.insertUser(name,salt)){
                result.setCode(300);
                user = new User(name, salt, 1);
                System.out.println("成功注册");
            }else{
                result.setCode(500);
                return JSONObject.toJSONString(result);
            }
        }else{
            result.setCode(100);
            return JSONObject.toJSONString(result);
        }
        session.setAttribute("user",user);
        return JSONObject.toJSONString(result);
    }

    /**
     * @Description: admin退出登录
     * @Param: []
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/16
     */
    @GetMapping("/adminLogout")
    public String adminLogout(){
        session.removeAttribute("admin");
        return "redirect:/toAdminLoginPage";
    }

    /**
     * @Description: 用户退出登录
     * @Param: []
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/16
     */
    @GetMapping("/userLogout")
    @ResponseBody
    public String userLogout(){
        session.removeAttribute("user");
        result.setCode(200);
        return JSONObject.toJSONString(result);
    }
}
